﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Model;

namespace iSun.BLL.Base
{
    /// <summary>
    /// http://www.w3school.com.cn/sql/sql_primarykey.asp
    /// </summary>
    public class DBTable : BaseBussnies
    {
        public SysTableField tableField
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public virtual bool AddTable(string TableName)
        {
            return false;
        }
        //ALTER TABLE [user] ADD column_name3 Varchar(100) not null
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="ColName"></param>
        /// <param name="ColType"></param>
        /// <param name="IsNull"></param>
        /// <returns></returns>
        public virtual bool AddCol(string TableName, string ColName, string ColType, string IsNull) { return false; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="ColName"></param>
        /// <param name="ColType"></param>
        /// <returns></returns>
        public virtual bool AddCol(string TableName, string ColName, string ColType) { return false; }
        public virtual bool AddCol(string TableName, string ColName, string ColType, int Length){return false;}
        public virtual bool AddCol(string TableName, string ColName, string ColType, int Length,string IsNull){return false;}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="ColName"></param>
        /// <param name="ColType"></param>
        /// <returns></returns>
        /// ALTER TABLE table_name DROP COLUMN column_name
        public virtual bool ModifyCol(string TableName, string ColName, string ColType){return false;}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="ColName"></param>
        /// <returns></returns>
        public virtual bool DropCol(string TableName, string ColName){return false;}

        // CREATE TABLE Persons
        //(
        //Id_P int NOT NULL,
        //LastName varchar(255) NOT NULL,
        //FirstName varchar(255),
        //Address varchar(255),
        //City varchar(255),
        //PRIMARY KEY (Id_P)
        //)

        //        CREATE TABLE Persons
        //(
        //Id_P int NOT NULL,
        //LastName varchar(255) NOT NULL,
        //FirstName varchar(255),
        //Address varchar(255),
        //City varchar(255),
        //CONSTRAINT uc_PersonID PRIMARY KEY (Id_P,LastName)
        //)

        public virtual bool CreateTable(string TableName, IList<SysTableField> FieldList) { return false; }
        public virtual bool CreateTable(string TableName){return false;}
        //EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'客户编码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CrmCustomer', @level2type=N'COLUMN',@level2name=N'Code'
        public virtual bool AddColNotes(string TableName, string FieldName, string Notes) { return false; }

        //ALTER TABLE Persons
        //ADD CONSTRAINT pk_PersonID PRIMARY KEY (Id_P,LastName)
        public virtual bool AddKey(string TableName, string Key)
        {
            return false;   
        }

        /// <summary>
        /// 通过别名获取表对象
        /// </summary>
        /// <param name="AlaisName">表别名</param>
        /// <returns>表对象</returns>
        public virtual SysTable  GetTableByAlaisName(string AlaisName)
        {
            return this.GetEntityByCondition<SysTable>("AlaisName='" + AlaisName + "'");
        }

        public virtual SysTable GetTableByTableName(string TableName)
        {
            return this.GetEntityByCondition<SysTable>("TableName='" + TableName + "'");
        }

        public virtual Guid GetTableIDByTableName(string TableName)
        {
            SysTable entity = GetTableByTableName(TableName);
            return entity == null ? Guid.Empty : entity.TableID;
        }
        /// <summary>
        /// 通过别名获取表名
        /// </summary>
        /// <param name="AlaisName">表别名</param>
        /// <returns>表名</returns>
        public virtual String GetTableNameByAlaisName(string AlaisName)
        {
            SysTable entity = GetTableByAlaisName(AlaisName);
            return entity == null ? "" : entity.TableName;
        }

        /// <summary>
        /// 通过别名获取TableID
        /// </summary>
        /// <param name="AlaisName">表别名</param>
        /// <returns>TableID</returns>
        public virtual Guid GetTableIDByAlaisName(string AlaisName)
        {
            SysTable entity = GetTableByAlaisName(AlaisName);
            return entity == null ? Guid.Empty : entity.TableID;
        }

        public virtual bool IsTableExists(string tableName)
        {
            return false;
        }
    }
}
